﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace AsyncLocalDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            Task.Run(async () =>
            {
                await ThreadShared.Test();
            });
            Console.ReadKey();
        }
    }

    public class ThreadShared
    {
        private string Id;
        static int _variable;
        static AsyncLocal<int> _asyncLocal = new AsyncLocal<int>();
        static ThreadLocal<int> _threadLocal = new ThreadLocal<int>();

        public static async Task Test()
        {
            Console.WriteLine($"T={Thread.CurrentThread.ManagedThreadId}");
            _variable = 1;
            _asyncLocal.Value = 1;
            _threadLocal.Value = 1;
            var o1 = Output("1");
            _variable = 2;
            _asyncLocal.Value = 2;
            _threadLocal.Value = 2;
            var o2 = Output("2");
            await Task.WhenAll(o1, o2);
        }

        public static async Task Output(string name)
        {
            await Task.Delay(1000);
            Console.WriteLine($"{name}: T={Thread.CurrentThread.ManagedThreadId}, V={_variable}, AL={_asyncLocal.Value}, TL={_threadLocal.Value}");
        }
    }

    public class Person
    {
        public string Id { get; set; }

        public string Name { get; set; }

        public Person()
        {
            Id = Guid.NewGuid().ToString("N");
            Name = "";
        }
    }
}
